
/****** Object:  Stored Procedure [dbo].tblUserGetList    Script Date: Thursday, October 27, 2011 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sptblUserGetList]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sptblUserGetList]
GO

	
	
	
/******************************************************************************
**		File: 
**		Name: [dbo].sptblUserGetList
**		Desc: 
**
**		Return values:
** 
**		Called by:   
**              
**		Parameters:
**		Input							Output
**     ----------							-----------
**
**		Auth: CodeSmith
**		Date: 27/10/2011 17:41:39
*******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:				Description:
**		--------		--------				-------------------------------------------
**    
*******************************************************************************/
CREATE PROCEDURE [dbo].sptblUserGetList
	@OrderBy varchar(50),
	@OrderDirection varchar(5),
	@Page int,
	@PageSize int,
	@TotalRecords int output
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

CREATE TABLE #TemptblUser (
	RowNumber INT IDENTITY (1, 1) NOT NULL,
	Peoplewise varchar(7)	
)

DECLARE @sql nvarchar(2000)
DECLARE @Top int

SET @Top = @Page*@PageSize
-- insert primary keys into temp table
SET @sql =	N'INSERT INTO #TemptblUser ([Peoplewise]) SELECT '
IF @PageSize > 0
   SET @sql = @sql + ' TOP ' + CAST(@Top as nvarchar)
SET @sql = @sql + ' [Peoplewise] FROM [dbo].[tblUser]  ORDER BY [' + @OrderBy + N'] ' + @OrderDirection
EXEC (@sql)


SELECT @TotalRecords = COUNT(*) FROM [tblUser]

SELECT
	[dbo].[tblUser].[Peoplewise],
	[UserName],
	[Email],
	[LastLoginDate],
	[Role],
	[Status]
FROM
	#TemptblUser AS tblTemp JOIN [dbo].[tblUser] ON
	tblTemp.Peoplewise = [dbo].[tblUser].Peoplewise 	
WHERE (@PageSize = 0) OR (@PageSize > 0 AND (@Page - 1)*@PageSize < RowNumber AND RowNumber <= @Page*@PageSize)
ORDER BY RowNumber

DROP TABLE #TemptblUser

GO

	
